<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 商品图片管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
              <el-input v-model="pagination.name" placeholder="商品名" class="handle-input mr10"></el-input>
              <el-input v-model="pagination.category" placeholder="分类ID" maxlength=1 class="handle-input mr10"></el-input>
              <el-button type="primary" icon="el-icon-search"  @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                >
                <el-table-column prop="good_id" label="商品ID" width="65" align="center"></el-table-column>
                <el-table-column prop="name" label="商品名" width="150" align="center"></el-table-column>

                <el-table-column label="首页分类图标(40*40)" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.icon"
                            :preview-src-list="[scope.row.icon]"
                        ></el-image>
                    </template>
                </el-table-column>

              <el-table-column label="顶部弹出图(160*110)" align="center">
                <template slot-scope="scope">
                  <el-image
                          class="table-td-thumb"
                          :src="scope.row.imageHead"
                          :preview-src-list="[scope.row.imageHead]"
                  ></el-image>
                </template>
              </el-table-column>

              <el-table-column label="首页商品图(200*200)" align="center">
                <template slot-scope="scope">
                  <el-image
                          class="table-td-thumb"
                          :src="scope.row.imageFoot"
                          :preview-src-list="[scope.row.imageFoot]"
                  ></el-image>
                </template>
              </el-table-column>

              <el-table-column label="购物车图(800*800)" align="center">
                <template slot-scope="scope">
                  <el-image
                          class="table-td-thumb"
                          :src="scope.row.image"
                          :preview-src-list="[scope.row.image]"
                  ></el-image>
                </template>
              </el-table-column>

              <el-table-column>
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑前三种图</el-button>
                    </template>
                </el-table-column>

              <el-table-column>
                <template slot-scope="scope">
                  <el-button
                          type="text"
                          icon="el-icon-edit"
                          @click="handleEditShop(scope.$index, scope.row)"
                  >编辑购物车图</el-button>
                </template>
              </el-table-column>

            </el-table>
          <div class="pagination">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                :total="pagination.total"
                @current-change="handlePageChange">
            </el-pagination>
          </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑1" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" label-width="120px">
                <el-form-item label="目录图片">
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:8081/upload/uploadImg"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess1"
                        :before-upload="beforeAvatarUpload">
                      <img v-if="form.icon" :src="form.icon" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="首页顶弹出图">
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:8081/upload/uploadImg"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess2"
                        :before-upload="beforeAvatarUpload">
                      <img v-if="form.imageHead" :src="form.imageHead" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="首页商品大图">
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:8081/upload/uploadImg"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess3"
                        :before-upload="beforeAvatarUpload">
                      <img v-if="form.imageFoot" :src="form.imageFoot" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确定修改</el-button>
            </span>
        </el-dialog>


      <!-- 编辑弹出框 -->
      <el-dialog title="编辑2" :visible.sync="editVisible2" width="50%">
        <el-form ref="formShop" :model="formShop" label-width="120px">
          <el-form-item label="购物车主图">
              <el-upload
                  class="avatar-uploader"
                  action="http://localhost:8081/upload/uploadImg"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess4"
                  :before-upload="beforeAvatarUpload">
                <img v-if="formShop.image" :src="formShop.image" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit2">确定修改</el-button>
        </span>
      </el-dialog>


    </div>
</template>

<script>
import {fetchData, getGood, getGoodByCategory, getGoodByName} from '../../api/index';
    import { getGoodPic, getGoodPicByCategory, getGoodPicByName, updGoodPic } from '../../api';
export default {
    name: 'GoodPic',
    data() {
        return {
          pagination: {
            name: '',
            category: '',
            currentPage: 1,
            pageSize: 10,
            total: 20,
          },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            editVisible2: false,
            pageTotal: 0,
            form: {},
            formShop:{},
            idx: -1,
            id: -1,
        };
    },
    created() {
        this.getData();
    },
    methods: {
        handleAvatarSuccess1(res, file) {
            this.form.icon = res.data
        },
        handleAvatarSuccess2(res, file) {
            this.form.imageHead = res.data
        },
        handleAvatarSuccess3(res, file) {
            this.form.imageFoot = res.data
        },
        handleAvatarSuccess4(res, file) {
          this.formShop.image = res.data
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 10;

            if (!isJPG) {
              this.$message.error('上传图片只能是 JPG/PNG 格式!');
            }
            if (!isLt2M) {
              this.$message.error('上传图片大小不能超过 10MB!');
            }
            return isJPG && isLt2M;
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            var params = {
              currentPage: this.pagination.currentPage,
              pageSize: this.pagination.pageSize,
              name: this.pagination.name,
              category: this.pagination.category
            };
            getGoodPic(params).then(res => {
                console.log(res);
                this.tableData = res.data.rows;
                this.pagination.total  = res.data.total;
            });
        },
        // 触发搜索按钮
        handleSearch() {
           this.getData();
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },

        handleEditShop(index, row){
            this.idx = index;
            this.formShop = row;
            this.editVisible2 = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行数据成功`);
            this.$set(this.tableData, this.idx, this.form);
            updGoodPic(this.form)
            getGoodPic(this.query).then(res => {
              console.log(res);
              this.tableData = res.list;
              this.pageTotal = res.pageTotal;
              this.currTotal = res.currTotal;
            });
        },
        saveEdit2() {
            this.editVisible2 = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.formShop);
            updGoodPic(this.formShop);
            getGoodPic(this.query).then(res => {
              console.log(res);
              this.tableData = res.list;
              this.pageTotal = res.pageTotal;
              this.currTotal = res.currTotal;
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.pagination.currentPage = val;
            this.getData();
        }
    }
};
</script>
<style scoped>
  .allshoppic {
    margin-top: 20px;
    margin-right: 30px;
    text-align: right;
  }
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  height: 178px;
  display: block;
}
</style>
